using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class HUD : MonoBehaviour
{

	private bool _init;
	private static HUD _instance;
	public static HUD Instance
	{
		get { return HUD._instance; }
	}

	private Transform _textureBille;
	private Transform _textureSlingShot;
	private Transform _textureFireCracker;
	private Transform _textureCostume;
	private TextMesh _nbBille;
	private TextMesh _nbSlingShot;
	private TextMesh _nbFireCracker;
	private TextMesh _nbCostume;
	private List<Transform> _selectedTexture = new List<Transform>();

	private const string nameTextureBille = "bille";
	private const string nameTextureSlingShot = "slingshot";
	private const string nameTextureFireCraker = "firecracker";
	private const string nameTextureCostume = "costume";
	private const string nameNbBille = "nbBilles";
	private const string nameNbSlingShot = "nbSlingShot";
	private const string nameNbFireCracker = "nbFireCracker";
	private const string nameNbCostume = "nbCostumes";
	private const string nameSelected = "selectHUD";


	public void Init()
	{
		_instance = this;

		GetObjects();

		_init = true;

		HideSelected();
	}

	// Update is called once per frame
	void Update()
	{

		if (!_init)
			return;

	}

	void GetObjects()
	{
		int nbChild = this.transform.GetChildCount();

		for (int i = 0; i < nbChild; ++i)
		{
			Transform child = this.transform.GetChild(i);
			switch (child.name)
			{
				case nameTextureBille:
					_textureBille = child;
					break;
				case nameTextureSlingShot:
					_textureSlingShot = child;
					break;
				case nameTextureFireCraker:
					_textureFireCracker = child;
					break;
				case nameTextureCostume:
					_textureCostume = child;
					break;
				case nameSelected:
					_selectedTexture.Add(child);
					break;
				case nameNbBille:
					_nbBille = child.GetComponent<TextMesh>();
					_nbBille.renderer.material.color = new Color(0.8f, 0.8f, 0.8f);
					_nbBille.fontStyle = FontStyle.Bold;
					_nbBille.anchor = TextAnchor.MiddleCenter;
					break;
				case nameNbSlingShot:
					_nbSlingShot = child.GetComponent<TextMesh>();
					_nbSlingShot.renderer.material.color = new Color(0.8f, 0.8f, 0.8f);
					_nbSlingShot.fontStyle = FontStyle.Bold;
					_nbSlingShot.anchor = TextAnchor.MiddleCenter;
					break;
				case nameNbFireCracker:
					_nbFireCracker = child.GetComponent<TextMesh>();
					_nbFireCracker.renderer.material.color = new Color(0.8f, 0.8f, 0.8f);
					_nbFireCracker.fontStyle = FontStyle.Bold;
					_nbFireCracker.anchor = TextAnchor.MiddleCenter;
					break;
				case nameNbCostume:
					_nbCostume = child.GetComponent<TextMesh>();
					_nbCostume.renderer.material.color = new Color(0.8f, 0.8f, 0.8f);
					_nbCostume.fontStyle = FontStyle.Bold;
					_nbCostume.anchor = TextAnchor.MiddleCenter;
					break;
				default:
					break;

			}
		}
	}

	public void HideSelected()
	{
		foreach (Transform select in _selectedTexture)
			select.renderer.enabled = false;
	}

	public void AddTrap(Const.TRAP_TYPE type, int quantity = 1)
	{
		Debug.Log(string.Format("changing hud value for type {0} by {1}", type, quantity));
		SelectTrap(type);
		switch (type)
		{
			case Const.TRAP_TYPE.BILLE:
				_nbBille.text = (int.Parse(_nbBille.text) + quantity).ToString();

				break;
			case Const.TRAP_TYPE.FIRECRACKER:
				_nbSlingShot.text = (int.Parse(_nbSlingShot.text) + quantity).ToString();
				break;
			case Const.TRAP_TYPE.SLINGSHOT:
				_nbFireCracker.text = (int.Parse(_nbFireCracker.text) + quantity).ToString();
				break;
			case Const.TRAP_TYPE.COSTUME:
				_nbCostume.text = (int.Parse(_nbCostume.text) + quantity).ToString();
				break;
		}
	}

	public void SelectTrap(Const.TRAP_TYPE type)
	{
		HideSelected();
		if (type != Const.TRAP_TYPE.NONE)
		{
			int num = (int)type;
			if (type == Const.TRAP_TYPE.COSTUME)
				num = 0;
			_selectedTexture[num].renderer.enabled = true;
		}
	}
}
